package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.accounting.dto.standardSub.SingleExportDTO;
import cc.mrbird.febs.accounting.dto.standardSub.StandardSubDTO;
import cc.mrbird.febs.accounting.dto.standardSub.SubMainDetailDTO;
import cc.mrbird.febs.accounting.entity.Project;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.StandardSub;
import cc.mrbird.febs.accounting.mapper.StandardSubMapper;
import cc.mrbird.febs.accounting.service.IStandardSubService;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.utils.PermissionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Objects;

/**
 * 标准明细科目表 Service实现
 *
 * @author baoyinlei
 * @date 2021-11-29 16:39:25
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StandardSubServiceImpl extends ServiceImpl<StandardSubMapper, StandardSub> implements IStandardSubService {

    private final StandardSubMapper standardSubMapper;

    @Override
    public IPage<StandardSub> findStandardSubs(QueryRequest request, StandardSub standardSub) {
        LambdaQueryWrapper<StandardSub> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<StandardSub> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<StandardSub> findStandardSubs(StandardSub standardSub) {
	    LambdaQueryWrapper<StandardSub> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        queryWrapper.eq(StandardSub::getIsDeleted, 0);
        queryWrapper.eq(StandardSub::getType, 2);
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createStandardSub(StandardSub standardSub) {
        this.save(standardSub);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStandardSub(StandardSub standardSub) {
        this.saveOrUpdate(standardSub);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStandardSub(StandardSub standardSub) {
        LambdaQueryWrapper<StandardSub> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    public List<StandardSub> queryByType(Integer type) {
        LambdaQueryWrapper<StandardSub> wrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(type)) {
            wrapper.eq(StandardSub::getType, type);
        }
        wrapper.eq(StandardSub::getIsDeleted,0);
        wrapper.orderByAsc(StandardSub::getLevle);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<SubMainDetailDTO> querySubMainDetail() {
        return this.baseMapper.querySubMainDetail();
    }


    @Override
    public List<SubMainDetailDTO> queryAllSubDetail(Long companyId, Long projectId, List<String> yearMonths) {
        return this.baseMapper.queryAllSubDetail(companyId, projectId, yearMonths);
    }

    public List<SubMainDetailDTO> queryAllSubDetailNoCompany(Long projectId, List<String> yearMonths) {
        return this.baseMapper.queryAllSubDetailNoCompany(projectId, yearMonths);
    }

    @Override
    public List<SingleExportDTO> querySubMainStandard(Long projectId, List<String> yearMonths) {
        return this.baseMapper.querySubMainStandard(projectId, yearMonths);
    }

    @Override
    public List<StandardSub> getStatisticsStandard() {
        return this.baseMapper.getStatisticsStandard();
    }

    @Override
    public List<StandardSubDTO> getDetailStatisticsStandard(List<String> projectIds, String mainName) {
        return this.baseMapper.getDetailStatisticsStandard(projectIds, mainName);
    }
}
